<!--
	Desc: 	企业信息表的编辑界面
	Author: caochaofeng
	Date: 	2018-08-19 17:21:27
-->

<template>
  <el-dialog
    :title="!dataForm.id ? '新增客户信息' : '修改客户信息'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="企业名称"></el-input>
      </el-form-item>

      <el-form-item label="地址" prop="location">
        <el-input v-model="dataForm.location" placeholder="地址"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="legalRepNm">
        <el-input v-model="dataForm.legalRepNm" placeholder="联系人"></el-input>
      </el-form-item>

      <el-form-item label="单位联系电话" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="单位联系电话"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import * as API  from '@/api/entp'

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          catCd: '',
          catCd: '2100.182',
          catNmCn: '收货企业',
          legalRepNm: '',
          mobile: ''
        },
        dataRule: {
          name: [
            {required: true, message: '企业名称不能为空', trigger: 'blur'}
          ],
          legalRepNm: [
            {required: true, message: '联系人姓名不能为空', trigger: 'blur'}
          ],
          mobile: [
            {required: true, message: '联系人电话不能为空', trigger: 'blur'}
          ],
          location: [
            {required: true, message: '地址不能为空', trigger: 'blur'}
          ],
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.info(this.dataForm.id).then((data) => {
              if (data && data.code === 0) {
                this.dataForm = data.entp

              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = this.dataForm
            var tick = !this.dataForm.id ? API.add(params) : API.update(params)
            tick.then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
